package com.pet.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.pet.config.SessionConstants;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.IBaseEntity;
import com.pet.shop.entity.user.UserInfo;
import com.pet.shop.service.IBaseService;
import com.pet.shop.utils.ApiResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaowenbang on 2018/1/29.
 */
public abstract class BaseApiController<T extends IBaseEntity> {

    protected static final Logger logger = LoggerFactory.getLogger(BaseApiController.class);

    public abstract IBaseService<T> getBaseService();

    protected Map<String, Object> success(Object data){
        return ApiResultUtil.success(data);
    }

    protected Map<String, Object> error(String msg){
        return ApiResultUtil.error_param(msg);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public T getEntity(@PathVariable("id")Long id){
        logger.info("根据ID {} 获取对象", id);
        return getBaseService().getEntity(id);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Map<String, Object> insertEntity(HttpServletRequest request, @RequestBody T entity){
        if(entity == null) return error("空对象无法新增");
        logger.info("新增对象 {}", JSONObject.toJSONString(entity));
        entity.setCreateTime(new Date());
        entity.setCreatorId(getSessionUser(request).getId());
        entity = getBaseService().insertEntity(entity);
        return success(entity);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> updateEntity(HttpServletRequest request, @RequestBody T entity){
        if(entity == null || entity.getId() == null ) return error("空对象无法更新");
        entity.setModifyTime(new Date());
        entity.setModifierId(getSessionUser(request).getId());
        getBaseService().updateEntity(entity);
        return success(null);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> removeEntity(@PathVariable("id")Long id){
        if(id == null) return error("空对象无法删除");
        getBaseService().deleteEntity(id);
        return success(null);
    }

    @RequestMapping(value = "/deleteList", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> removeEntityList(@RequestBody List<Long> idList){
        if(idList == null) return error("空对象无法删除");
        getBaseService().deleteEntityList(idList);
        return success(null);
    }

    @RequestMapping("/queryPage")
    @ResponseBody
    public Map<String, Object> queryByPage(HttpServletRequest request, @RequestBody PageDto<T> pageDto){
        logger.info("分页参数 {}", JSONObject.toJSONString(pageDto));
        pageDto =  getBaseService().queryPage(pageDto);
        logger.info("分页返回 total: {}， results: {}", pageDto.getTotalRecord(), JSONObject.toJSONString(pageDto.getResults()));
        return ApiResultUtil.success_page(pageDto);
    }

    protected UserInfo getSessionUser(HttpServletRequest request){
        return (UserInfo) request.getSession().getAttribute(SessionConstants.APP_SESSION_KEY);
    }
}
